package com.chebaohui.iqs.service.impl.deal.union.sdk;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 银联时间工具类
 * 
 * @author xielonghai
 *
 */
public class TimeTool {

	/**
	 * 默认时间格式
	 */
	public static final String sdf_default = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 银联所需时间格式，商户发送交易时间 格式:YYYYMMDDhhmmss
	 */
	public static final String sdf_union = "yyyyMMddHHmmss";
	public static final String sdf_union2 = "yyyyMMdd";
	public static final String sdf_union3 = "HHmmss";

	/**
	 * 获取指定格式的当前时间
	 * 
	 * @param sdf
	 * @return
	 */
	public static String getCurrentTime(String sdf) {
		return new SimpleDateFormat(sdf).format(new Date());
	}

	/**
	 * 根据long值得到知道格式时间
	 * 
	 * @param time
	 * @return
	 */
	public static String getCurrentTime(String sdf, long time) {
		return new SimpleDateFormat(sdf).format(new Date(time));
	}

	/**
	 * 获取超时时间
	 * 
	 * @param time
	 * @param sdf
	 * @param out
	 * @return
	 * @throws Exception
	 */
	public static String getTimeout(String time, String sdf, String outHour) throws Exception {
		try {
			SimpleDateFormat sdfs = new SimpleDateFormat(sdf);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sdfs.parse(time));
			calendar.add(Calendar.MINUTE, Integer.valueOf(outHour));
			return new SimpleDateFormat(sdf).format(calendar.getTime());
		} catch (Exception e) {
			throw new Exception("获取超时时间错误。time:" + time + ",sdf:" + sdf + ",outHour:" + outHour);
		}
	}

	/**
	 * 将已有格式的时间转化为指定格式时间
	 * 
	 * @param time
	 * @param time_sdf
	 * @param sdf
	 * @return
	 */
	public static String timeToTime(String time, String timeSdf, String sdf) throws Exception {
		try {
			SimpleDateFormat org = new SimpleDateFormat(timeSdf);
			return new SimpleDateFormat(sdf).format(org.parse(time));
		} catch (Exception e) {
			throw new Exception("将已有格式的时间转化为指定格式时间错误。time:" + time + ",timeSdf:" + timeSdf + ",sdf:" + sdf);
		}
	}

	/**
	 * 将已有格式的时间加上已有天数
	 * 
	 * @param time
	 * @param sdf
	 * @param day
	 * @return
	 * @throws Exception
	 */
	public static String timeToDay(String time, String sdf, int day) throws Exception {
		try {
			SimpleDateFormat org = new SimpleDateFormat(sdf);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(org.parse(time));
			calendar.add(Calendar.DATE, day);
			return org.format(calendar.getTime());
		} catch (Exception e) {
			throw new Exception("将已有格式的时间加上已有天数。time:" + time + ",sdf:" + sdf + ",day:" + day);
		}
	}
}
